package com.wzx.user.sdk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wzx.common.exception.LeadNewsException;
import com.wzx.user.mapper.UserMapper;
import com.wzx.user.mapper.WalletMapper;
import com.wzx.user.pojo.User;
import com.wzx.user.pojo.Wallet;
import com.wzx.user.sdk.common.identity.java.IdentityUtils;
import com.wzx.user.sdk.common.identity.spring.IdAutoConfiguration;
import com.wzx.user.sdk.common.identity.spring.UserAuthForm;
import com.wzx.user.sdk.service.IdentityService;
import com.wzx.user.service.CommonService;
import org.aspectj.weaver.ast.Var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.Map;

/**
 * @author wzx
 * @version 1.0
 * @date 2022/10/31 - 11:46
 * @description 标题
 */
@Service
public class IdentityServiceImpl implements IdentityService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private WalletMapper walletMapper;

    @Autowired
    private CommonService commonService;

    @Override
    public String identifyVerify(UserAuthForm userAuthForm) throws Exception {
        if (StringUtils.isEmpty(userAuthForm.getPassword())) {
            throw new IllegalArgumentException("密码设置异常！");
        }

        //调用实名认证接口
        Map<String, Object> stringObjectMap = IdentityUtils.identityUtils(userAuthForm.getRealName(), userAuthForm.getIdCard());
        if (Integer.parseInt(stringObjectMap.get("code").toString()) != 1) {
            throw new IllegalArgumentException(stringObjectMap.get("message").toString());
        }

        //将身份信息存储到数据库
        userMapper.updateIdentity(userAuthForm.getRealName(), userAuthForm.getIdCard(), userAuthForm.getUserId());
        //设置支付密码
        Wallet wallet = new Wallet();

        //密码加密
        String encryption = commonService.encryption(userAuthForm.getUserId(), userAuthForm.getPassword());
        wallet.setPassword(encryption);
        wallet.setBalance(0);
        wallet.setUid(userAuthForm.getUserId());
        walletMapper.insert(wallet);

        return "认证成功";
    }

    @Override
    public boolean judgeCheck(Integer userId) throws LeadNewsException {
        User user = userMapper.selectById(userId);
        if (user == null) {
            throw new LeadNewsException("未查找的该用户！");
        }
        String authentication = user.getAuthentication();
        return authentication.equals("1");
    }

    @Override
    public String queryName(Integer userId) throws LeadNewsException {
        User user = userMapper.selectById(userId);
        if (user == null) {
            throw new LeadNewsException("未查找的该用户！");
        }
        //姓名处理
        String name = user.getIdName();
        String subName = name.substring(0, 1);
        int addNameLength = name.length() - 1;
        StringBuilder addName = new StringBuilder();
        for (int i = 0; i < addNameLength; i++) {
            addName.append("*");
        }

        return subName + addName;
    }
}
